<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Think;

class Rsa{
    /*
     * 测试服务器：118.190.137.64
     * */
    public $privateKey = '-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCtCqBJaNhvTv2DIjM7jQZvFiVHmsVkphcgTEoGr1+FybH4I9OG
D940aQ8KLui3HK2K+4BhsoSVK3t5hIjqZxqPS/yppbTyW0Xnd30irqK8RT/uJhu5
BYSPeqT3JhY8gHm5/+Q4dPtD0z5h0L4QSfAwgylQU3GcqMcaBKysvUeDFwIDAQAB
AoGAUOtSL2IHLG4c4RDCMovtQU9y2XxH6Jw+3kyupVenbNH4FpjyVpPfDlU+wj1T
eCXCBec8jvNDStqNoDLZL/xND4bJOUuxn1NhTnYAohAxF6EQnRv1RkbuDqMvEe83
8q9YejJc3EFQDglLSp1AE22aFwv95F3XWSLmiY2NYcYE0gECQQDXyeAYbOfH3Twi
/H3icRuMajJCZf4MjFNPVEP9YTgFktiozexcN2fdd+emtMOFu7GdPQTVe6BmVqy0
ohlxb0gXAkEAzUmEAQP0UIcrBcoo2KFlGH5Xa8WMoJteMrOvHzxOdBNywxfhEwYa
+16Dph36B+UaBByoJdQuJIVSqCr9yaN9AQJAOtX7OQ2woMkMlYl8lVo7paxq6bm/
bBJywKV8KEDysmpYkqYa9QXSVwtTaRvOV89WXUX+5Gph0KV/T30HLOunnwJAICab
Xe1YniQNQOLgcs0UIDnpRx+xXpX2ssohayruvo8tMhDTVz9limjLBYE8X3AqGurC
CuqoL2ASCfRxutq4AQJBAKnG0GgdesheIeBrEK9FAzU7di9e0iqM9skrdYAdcHFX
s9Ly7TkM/pMpM08nr78Y299tcTXT4XvrA8tsAVb9CPY=
-----END RSA PRIVATE KEY-----
';

   /* public $publicKey = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtCqBJaNhvTv2DIjM7jQZvFiVH
msVkphcgTEoGr1+FybH4I9OGD940aQ8KLui3HK2K+4BhsoSVK3t5hIjqZxqPS/yp
pbTyW0Xnd30irqK8RT/uJhu5BYSPeqT3JhY8gHm5/+Q4dPtD0z5h0L4QSfAwgylQ
U3GcqMcaBKysvUeDFwIDAQAB
-----END PUBLIC KEY-----
';*/
    public $publicKey =   '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtCqBJaNhvTv2DIjM7jQZvFiVH
msVkphcgTEoGr1+FybH4I9OGD940aQ8KLui3HK2K+4BhsoSVK3t5hIjqZxqPS/yp
pbTyW0Xnd30irqK8RT/uJhu5BYSPeqT3JhY8gHm5/+Q4dPtD0z5h0L4QSfAwgylQ
U3GcqMcaBKysvUeDFwIDAQAB
-----END PUBLIC KEY-----
';

    public function __construct()
    {
        $resource = openssl_pkey_new();
        openssl_pkey_export($resource, $this->privateKey);
        $detail = openssl_pkey_get_details($resource);
        $this->publicKey = $detail['key'];
    }

    public function publicEncrypt($data, $publicKey)
    {
        openssl_public_encrypt($data, $encrypted, $publicKey);
        return $encrypted;
    }

    public function publicDecrypt($data, $publicKey)
    {
        openssl_public_decrypt($data, $decrypted, $publicKey);
        return $decrypted;
    }

    public function privateEncrypt($data, $privateKey)
    {
        openssl_private_encrypt($data, $encrypted, $privateKey);
        return $encrypted;
    }

    public function privateDecrypt($data, $privateKey)
    {
        openssl_private_decrypt($data, $decrypted, $privateKey);
        return $decrypted;
    }


}
